/*
 * Copyright 2020 Makani Technologies LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "avionics/common/crc.h"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>

// This table is calculated with the standard CRC-32 polynomial 0xEDB88320.
static const uint32_t kCrc32Table[256] = {
  0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL, 0x076DC419L,
  0x706AF48FL, 0xE963A535L, 0x9E6495A3L, 0x0EDB8832L, 0x79DCB8A4L,
  0xE0D5E91EL, 0x97D2D988L, 0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L,
  0x90BF1D91L, 0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL,
  0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L, 0x136C9856L,
  0x646BA8C0L, 0xFD62F97AL, 0x8A65C9ECL, 0x14015C4FL, 0x63066CD9L,
  0xFA0F3D63L, 0x8D080DF5L, 0x3B6E20C8L, 0x4C69105EL, 0xD56041E4L,
  0xA2677172L, 0x3C03E4D1L, 0x4B04D447L, 0xD20D85FDL, 0xA50AB56BL,
  0x35B5A8FAL, 0x42B2986CL, 0xDBBBC9D6L, 0xACBCF940L, 0x32D86CE3L,
  0x45DF5C75L, 0xDCD60DCFL, 0xABD13D59L, 0x26D930ACL, 0x51DE003AL,
  0xC8D75180L, 0xBFD06116L, 0x21B4F4B5L, 0x56B3C423L, 0xCFBA9599L,
  0xB8BDA50FL, 0x2802B89EL, 0x5F058808L, 0xC60CD9B2L, 0xB10BE924L,
  0x2F6F7C87L, 0x58684C11L, 0xC1611DABL, 0xB6662D3DL, 0x76DC4190L,
  0x01DB7106L, 0x98D220BCL, 0xEFD5102AL, 0x71B18589L, 0x06B6B51FL,
  0x9FBFE4A5L, 0xE8B8D433L, 0x7807C9A2L, 0x0F00F934L, 0x9609A88EL,
  0xE10E9818L, 0x7F6A0DBBL, 0x086D3D2DL, 0x91646C97L, 0xE6635C01L,
  0x6B6B51F4L, 0x1C6C6162L, 0x856530D8L, 0xF262004EL, 0x6C0695EDL,
  0x1B01A57BL, 0x8208F4C1L, 0xF50FC457L, 0x65B0D9C6L, 0x12B7E950L,
  0x8BBEB8EAL, 0xFCB9887CL, 0x62DD1DDFL, 0x15DA2D49L, 0x8CD37CF3L,
  0xFBD44C65L, 0x4DB26158L, 0x3AB551CEL, 0xA3BC0074L, 0xD4BB30E2L,
  0x4ADFA541L, 0x3DD895D7L, 0xA4D1C46DL, 0xD3D6F4FBL, 0x4369E96AL,
  0x346ED9FCL, 0xAD678846L, 0xDA60B8D0L, 0x44042D73L, 0x33031DE5L,
  0xAA0A4C5FL, 0xDD0D7CC9L, 0x5005713CL, 0x270241AAL, 0xBE0B1010L,
  0xC90C2086L, 0x5768B525L, 0x206F85B3L, 0xB966D409L, 0xCE61E49FL,
  0x5EDEF90EL, 0x29D9C998L, 0xB0D09822L, 0xC7D7A8B4L, 0x59B33D17L,
  0x2EB40D81L, 0xB7BD5C3BL, 0xC0BA6CADL, 0xEDB88320L, 0x9ABFB3B6L,
  0x03B6E20CL, 0x74B1D29AL, 0xEAD54739L, 0x9DD277AFL, 0x04DB2615L,
  0x73DC1683L, 0xE3630B12L, 0x94643B84L, 0x0D6D6A3EL, 0x7A6A5AA8L,
  0xE40ECF0BL, 0x9309FF9DL, 0x0A00AE27L, 0x7D079EB1L, 0xF00F9344L,
  0x8708A3D2L, 0x1E01F268L, 0x6906C2FEL, 0xF762575DL, 0x806567CBL,
  0x196C3671L, 0x6E6B06E7L, 0xFED41B76L, 0x89D32BE0L, 0x10DA7A5AL,
  0x67DD4ACCL, 0xF9B9DF6FL, 0x8EBEEFF9L, 0x17B7BE43L, 0x60B08ED5L,
  0xD6D6A3E8L, 0xA1D1937EL, 0x38D8C2C4L, 0x4FDFF252L, 0xD1BB67F1L,
  0xA6BC5767L, 0x3FB506DDL, 0x48B2364BL, 0xD80D2BDAL, 0xAF0A1B4CL,
  0x36034AF6L, 0x41047A60L, 0xDF60EFC3L, 0xA867DF55L, 0x316E8EEFL,
  0x4669BE79L, 0xCB61B38CL, 0xBC66831AL, 0x256FD2A0L, 0x5268E236L,
  0xCC0C7795L, 0xBB0B4703L, 0x220216B9L, 0x5505262FL, 0xC5BA3BBEL,
  0xB2BD0B28L, 0x2BB45A92L, 0x5CB36A04L, 0xC2D7FFA7L, 0xB5D0CF31L,
  0x2CD99E8BL, 0x5BDEAE1DL, 0x9B64C2B0L, 0xEC63F226L, 0x756AA39CL,
  0x026D930AL, 0x9C0906A9L, 0xEB0E363FL, 0x72076785L, 0x05005713L,
  0x95BF4A82L, 0xE2B87A14L, 0x7BB12BAEL, 0x0CB61B38L, 0x92D28E9BL,
  0xE5D5BE0DL, 0x7CDCEFB7L, 0x0BDBDF21L, 0x86D3D2D4L, 0xF1D4E242L,
  0x68DDB3F8L, 0x1FDA836EL, 0x81BE16CDL, 0xF6B9265BL, 0x6FB077E1L,
  0x18B74777L, 0x88085AE6L, 0xFF0F6A70L, 0x66063BCAL, 0x11010B5CL,
  0x8F659EFFL, 0xF862AE69L, 0x616BFFD3L, 0x166CCF45L, 0xA00AE278L,
  0xD70DD2EEL, 0x4E048354L, 0x3903B3C2L, 0xA7672661L, 0xD06016F7L,
  0x4969474DL, 0x3E6E77DBL, 0xAED16A4AL, 0xD9D65ADCL, 0x40DF0B66L,
  0x37D83BF0L, 0xA9BCAE53L, 0xDEBB9EC5L, 0x47B2CF7FL, 0x30B5FFE9L,
  0xBDBDF21CL, 0xCABAC28AL, 0x53B39330L, 0x24B4A3A6L, 0xBAD03605L,
  0xCDD70693L, 0x54DE5729L, 0x23D967BFL, 0xB3667A2EL, 0xC4614AB8L,
  0x5D681B02L, 0x2A6F2B94L, 0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL,
  0x2D02EF8DL
};

// This table is calculated with the CRC-24 Qualcomm polynomial 0x1864CFB.
static const uint32_t kCrc24QualcommTable[256] = {
  0x00000000L, 0x01864CFBL, 0x028AD50DL, 0x030C99F6L, 0x0493E6E1L,
  0x0515AA1AL, 0x061933ECL, 0x079F7F17L, 0x08A18139L, 0x0927CDC2L,
  0x0A2B5434L, 0x0BAD18CFL, 0x0C3267D8L, 0x0DB42B23L, 0x0EB8B2D5L,
  0x0F3EFE2EL, 0x10C54E89L, 0x11430272L, 0x124F9B84L, 0x13C9D77FL,
  0x1456A868L, 0x15D0E493L, 0x16DC7D65L, 0x175A319EL, 0x1864CFB0L,
  0x19E2834BL, 0x1AEE1ABDL, 0x1B685646L, 0x1CF72951L, 0x1D7165AAL,
  0x1E7DFC5CL, 0x1FFBB0A7L, 0x200CD1E9L, 0x218A9D12L, 0x228604E4L,
  0x2300481FL, 0x249F3708L, 0x25197BF3L, 0x2615E205L, 0x2793AEFEL,
  0x28AD50D0L, 0x292B1C2BL, 0x2A2785DDL, 0x2BA1C926L, 0x2C3EB631L,
  0x2DB8FACAL, 0x2EB4633CL, 0x2F322FC7L, 0x30C99F60L, 0x314FD39BL,
  0x32434A6DL, 0x33C50696L, 0x345A7981L, 0x35DC357AL, 0x36D0AC8CL,
  0x3756E077L, 0x38681E59L, 0x39EE52A2L, 0x3AE2CB54L, 0x3B6487AFL,
  0x3CFBF8B8L, 0x3D7DB443L, 0x3E712DB5L, 0x3FF7614EL, 0x4019A3D2L,
  0x419FEF29L, 0x429376DFL, 0x43153A24L, 0x448A4533L, 0x450C09C8L,
  0x4600903EL, 0x4786DCC5L, 0x48B822EBL, 0x493E6E10L, 0x4A32F7E6L,
  0x4BB4BB1DL, 0x4C2BC40AL, 0x4DAD88F1L, 0x4EA11107L, 0x4F275DFCL,
  0x50DCED5BL, 0x515AA1A0L, 0x52563856L, 0x53D074ADL, 0x544F0BBAL,
  0x55C94741L, 0x56C5DEB7L, 0x5743924CL, 0x587D6C62L, 0x59FB2099L,
  0x5AF7B96FL, 0x5B71F594L, 0x5CEE8A83L, 0x5D68C678L, 0x5E645F8EL,
  0x5FE21375L, 0x6015723BL, 0x61933EC0L, 0x629FA736L, 0x6319EBCDL,
  0x648694DAL, 0x6500D821L, 0x660C41D7L, 0x678A0D2CL, 0x68B4F302L,
  0x6932BFF9L, 0x6A3E260FL, 0x6BB86AF4L, 0x6C2715E3L, 0x6DA15918L,
  0x6EADC0EEL, 0x6F2B8C15L, 0x70D03CB2L, 0x71567049L, 0x725AE9BFL,
  0x73DCA544L, 0x7443DA53L, 0x75C596A8L, 0x76C90F5EL, 0x774F43A5L,
  0x7871BD8BL, 0x79F7F170L, 0x7AFB6886L, 0x7B7D247DL, 0x7CE25B6AL,
  0x7D641791L, 0x7E688E67L, 0x7FEEC29CL, 0x803347A4L, 0x81B50B5FL,
  0x82B992A9L, 0x833FDE52L, 0x84A0A145L, 0x8526EDBEL, 0x862A7448L,
  0x87AC38B3L, 0x8892C69DL, 0x89148A66L, 0x8A181390L, 0x8B9E5F6BL,
  0x8C01207CL, 0x8D876C87L, 0x8E8BF571L, 0x8F0DB98AL, 0x90F6092DL,
  0x917045D6L, 0x927CDC20L, 0x93FA90DBL, 0x9465EFCCL, 0x95E3A337L,
  0x96EF3AC1L, 0x9769763AL, 0x98578814L, 0x99D1C4EFL, 0x9ADD5D19L,
  0x9B5B11E2L, 0x9CC46EF5L, 0x9D42220EL, 0x9E4EBBF8L, 0x9FC8F703L,
  0xA03F964DL, 0xA1B9DAB6L, 0xA2B54340L, 0xA3330FBBL, 0xA4AC70ACL,
  0xA52A3C57L, 0xA626A5A1L, 0xA7A0E95AL, 0xA89E1774L, 0xA9185B8FL,
  0xAA14C279L, 0xAB928E82L, 0xAC0DF195L, 0xAD8BBD6EL, 0xAE872498L,
  0xAF016863L, 0xB0FAD8C4L, 0xB17C943FL, 0xB2700DC9L, 0xB3F64132L,
  0xB4693E25L, 0xB5EF72DEL, 0xB6E3EB28L, 0xB765A7D3L, 0xB85B59FDL,
  0xB9DD1506L, 0xBAD18CF0L, 0xBB57C00BL, 0xBCC8BF1CL, 0xBD4EF3E7L,
  0xBE426A11L, 0xBFC426EAL, 0xC02AE476L, 0xC1ACA88DL, 0xC2A0317BL,
  0xC3267D80L, 0xC4B90297L, 0xC53F4E6CL, 0xC633D79AL, 0xC7B59B61L,
  0xC88B654FL, 0xC90D29B4L, 0xCA01B042L, 0xCB87FCB9L, 0xCC1883AEL,
  0xCD9ECF55L, 0xCE9256A3L, 0xCF141A58L, 0xD0EFAAFFL, 0xD169E604L,
  0xD2657FF2L, 0xD3E33309L, 0xD47C4C1EL, 0xD5FA00E5L, 0xD6F69913L,
  0xD770D5E8L, 0xD84E2BC6L, 0xD9C8673DL, 0xDAC4FECBL, 0xDB42B230L,
  0xDCDDCD27L, 0xDD5B81DCL, 0xDE57182AL, 0xDFD154D1L, 0xE026359FL,
  0xE1A07964L, 0xE2ACE092L, 0xE32AAC69L, 0xE4B5D37EL, 0xE5339F85L,
  0xE63F0673L, 0xE7B94A88L, 0xE887B4A6L, 0xE901F85DL, 0xEA0D61ABL,
  0xEB8B2D50L, 0xEC145247L, 0xED921EBCL, 0xEE9E874AL, 0xEF18CBB1L,
  0xF0E37B16L, 0xF16537EDL, 0xF269AE1BL, 0xF3EFE2E0L, 0xF4709DF7L,
  0xF5F6D10CL, 0xF6FA48FAL, 0xF77C0401L, 0xF842FA2FL, 0xF9C4B6D4L,
  0xFAC82F22L, 0xFB4E63D9L, 0xFCD11CCEL, 0xFD575035L, 0xFE5BC9C3L,
  0xFFDD8538L
};

// If data is NULL, that just means we're checksumming from the beginning of
// flash.  So far, this is the only CRC used there, so the others still assert
// on NULL.
uint32_t Crc32(uint32_t init, int32_t length, const void *data) {
  assert(length >= 0);

  const uint8_t *buf = (const uint8_t *)data;
  uint32_t crc = ~init;
  for (int32_t i = 0; i < length; ++i) {
    crc = kCrc32Table[(crc ^ buf[i]) & 0xFF] ^ (crc >> 8);
  }
  return ~crc;
}

uint32_t Crc24Qualcomm(uint32_t init, int32_t length, const void *data) {
  assert(length >= 0);
  assert(data != NULL);

  const uint8_t *buf = (const uint8_t *)data;
  uint32_t crc = init;
  for (int32_t i = 0; i < length; ++i) {
    crc = (crc << 8) ^ kCrc24QualcommTable[(buf[i] ^ (crc >> 16)) & 0xFF];
  }
  return crc & 0x00FFFFFF;
}

uint16_t Crc16Ccitt(uint16_t init, int32_t length, const void *data) {
  assert(length >= 0);
  assert(data != NULL);

  uint16_t crc = init;
  const uint8_t *buf = (const uint8_t *)data;
  for (int32_t i = 0; i < length; ++i) {
    uint32_t x = ((crc >> 8) ^ buf[i]) & 0xFF;
    x ^= x >> 4;
    x ^= ((uint32_t)crc << 8) ^ (x << 12) ^ (x << 5);
    crc = x & 0xFFFF;
  }
  return crc;
}
